import { updateUserZyz } from '../../../api/user'
import { uploadFileName, wxUpload } from '../../../utils/upload'

Page({
  data: {
    zyzUrls: [],
    zyz: ''
  },

  onLoad: function (options) {
    this.setData({
      zyz: getApp().globalData.zyz,
      zyzUrls: [{url:getApp().globalData.zyz.zyzUrl}]
    })
  },

  afterRead(event) {
    const _this = this;
    const { file } = event.detail;
    const fileName = uploadFileName(file.path);

    wxUpload({
      biz: 'CERTIFICATE',
      fileName: fileName,
      size: file.size,
      filePath: file.path
    }).then(res => {
      _this.setData({
        zyzUrls: [{url: res.signatureUrl}],
        zyz: {
          applyStatus: _this.data.zyz.applyStatus,
          zyzUrl: res.uri
        }
      })
    })
  },

  removeImage(e) {
    this.setData({
      zyzUrls: [],
      zyz: {}
    })
  },

  done(e) {
    const pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    console.log(this.data.zyz)
    updateUserZyz(this.data.zyz).then(res => {
      prevPage.setData({
        zyz: this.data.zyz
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  }

})
